package com.ejuornal.dao.impl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ListModel;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ejuornal.dao.RoleDao;
import com.ejuornal.bean.Role;
/**
 * RoleDao的实现类
 * @author Liang
 *
 * @param <T>
 * 
 * 2013-1-26
 */
@Component("roleDao")
public class RoleDaoImpl extends DaoSupport<Role> implements RoleDao {

	@Override
	public boolean batchUpdate() {
		final List<String> roles = new ArrayList<String>();
		final List<String> roledescs = new ArrayList<String>();
		roles.add("黑名单或者未激活用户"); roledescs.add("此角色默认编号为1");
		roles.add("普通会员");            roledescs.add("此角色默认编号为2");
		roles.add("VIP会员");             roledescs.add("此角色默认编号为3");
		roles.add("主编");                roledescs.add("此角色默认编号为4");
		roles.add("编辑");                roledescs.add("此角色默认编号为5");
		roles.add("编委");                roledescs.add("此角色默认编号为6");
		roles.add("超级管理员");          roledescs.add("此角色默认编号为7");
		roles.add("会员管理员");          roledescs.add("此角色默认编号为8");
		roles.add("期刊管理员");          roledescs.add("此角色默认编号为9");
		String sql ="insert into t_role(roleId, roleName,roleDesc) value(?,?,?)";
		getJdbcTemplate().batchUpdate(sql, 
				 new BatchPreparedStatementSetter(){
					@Override
					public int getBatchSize() {
						return 0;
					}
					@Override
					public void setValues(PreparedStatement ps, int i)
							throws SQLException {
						ps.setInt(1, i+1);
						ps.setString(2, roles.get(i));
						ps.setString(3, roledescs.get(i));
					}
				}
				);
		return true;
	}

	
	@SuppressWarnings("unchecked")
	public List<Role> list() {
		String sql = "select * from t_role";
		@SuppressWarnings("rawtypes")
		List<Role> roles = getJdbcTemplate().query(sql, 
				new RowMapper() {
					public Object mapRow(ResultSet rs, int arg1)
							throws SQLException {
						Role role = new Role();
						role.setRoleId(rs.getInt("roleId"));
						role.setRoleName(rs.getString("roleName"));
						role.setRoleDesc(rs.getString("roleDesc"));
						return role;
					}
			
		}
		);
		return roles;
	}


}
